﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCrawlerLib;

namespace WCrawler.Controls
{
    public partial class ucTemplate : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        protected void btnAddTemplate_Click(object sender, EventArgs e)
        {
            Template objTemplate = new Template();
            objTemplate.AddTemplate(txtTemplate.Text);
            treeViewTemplate.Nodes[0].ChildNodes.Clear();
            treeViewTemplate.Nodes[0].PopulateOnDemand = true;
            treeViewTemplate.Nodes[0].SelectAction = TreeNodeSelectAction.SelectExpand;
        }

        /// <summary>
        /// Get selected node in treeview
        /// </summary>
        public TreeNode SelectedNode
        {
            get
            {
                return this.treeViewTemplate.SelectedNode;
            }
        }

        public void PopulateRootTopics(TreeNode node)
        {
            Template objTemplate = new Template();
            var query = objTemplate.GetAllTemplates();
            foreach (Template value in query)
            {
                TreeNode newNode = new TreeNode(value.TemplateName, value.TemplateId.ToString());
                //newNode.PopulateOnDemand = true;
                newNode.SelectAction = TreeNodeSelectAction.Select;
                node.ChildNodes.Add(newNode);
            }

        }

        //public void PopulateChildTopics(TreeNode node)
        //{
        //    Topic objTopic = new Topic();
        //    var query = objTopic.GetChildTopics(int.Parse(node.Value));
        //    foreach (Topic value in query)
        //    {
        //        TreeNode newNode = new TreeNode(value.TopicName, value.TopicId.ToString());
        //        var childNodes = objTopic.GetChildTopics(value.TopicId);
        //        newNode.PopulateOnDemand = true;
        //        newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
        //        node.ChildNodes.Add(newNode);
        //    }
        //}

        protected void treeView_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                PopulateRootTopics(e.Node);
                e.Node.Selected = true;
                e.Node.Expand();
                //switch (e.Node.Depth)
                //{
                //    case 0:
                //        PopulateRootTopics(e.Node);
                //        e.Node.Selected = true;
                //        break;
                //    default:
                //        PopulateChildTopics(e.Node);
                //        break;
                //}
            }
        }

        ///// <summary>
        ///// Get all topics in selected node
        ///// </summary>
        ///// <param name="parentNode"></param>
        ///// <returns></returns>
        //public string GetTemplates(TreeNode parentNode)
        //{

        //    string query = parentNode.Value;
        //    Template objTemplate = new Template();
        //    if (parentNode.Parent != null)
                
        //    var query = objTemplate.GetAllTemplates();
        //    else
        //        topicList = objTemplate.GetAllTopics();

        //    foreach (Topic obj in topicList)
        //    {
        //        query += "|" + obj.TopicId.ToString();
        //    }

        //    return query;

        //    //string query = parentNode.Value;
        //    //TreeNodeCollection nodeCollection = parentNode.ChildNodes;
        //    //foreach (TreeNode node in nodeCollection)
        //    //{
        //    //    query += "|" + GetTemplates(node);
        //    //}
        //    //return query;
        //}

        public event EventHandler SeletedTemplateIndexChanged;
        protected void treeView_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (SeletedTemplateIndexChanged != null)
                SeletedTemplateIndexChanged(sender, e);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            mdlPopup.Show();
        }

        
    }
}